﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.Xml
Imports System.Web
Imports System.Text

Namespace Rss

    ''' <summary>
    ''' Rss 2.0 Item without extensions
    ''' </summary>
    Public Class RssItem
        Inherits RssBase

#Region "Constructors"

        Public Sub New(ByVal title As String, ByVal link As Uri, _
                       ByVal description As String, _
                       Optional ByVal author As String = "", _
                       Optional ByVal pubDate As DateTime = Nothing)

            Me.title = title
            Me.link = link
            Me.description = description
            Me.author = author
            Me.guid = Me.link.ToString
            Me.pubDate = pubDate
            Me.category = New List(Of RssCategory)

        End Sub

        Public Sub New(ByVal title As String, ByVal link As Uri, ByVal description As String, _
                       ByVal author As String, ByVal categories As List(Of RssCategory), ByVal comments As String, _
                       ByVal enclosure As RssItemEnclosure, ByVal guid As String, ByVal pubDate As DateTime, ByVal source As RssItemSource)

            Me.title = title
            Me.link = link
            Me.description = description
            Me.author = author
            Me.category = categories
            Me.comments = comments
            Me.enclosure = enclosure
            Me.guid = guid
            Me.pubDate = pubDate
            Me.source = source

        End Sub


#End Region

        ''' <summary>
        ''' Returns an instantiation of RssItem given rss xml
        ''' </summary>
        Public Shared Function GetRssItem(ByVal item As XmlNode, Optional ByVal title As String = "", Optional ByVal url As String = "") As RssItem

            Dim source As RssItemSource = Nothing
            If Not String.IsNullOrEmpty(title) And Not String.IsNullOrEmpty("url") Then source = New RssItemSource(title, url)

            Return New RssItem( _
                GetChildItemInnerText(item, "title"), _
                GetChildItemInnerTextUri(item, "link"), _
                GetChildItemInnerText(item, "description"), _
                GetChildItemInnerText(item, "author"), _
                RssCategory.GetRssCategories(item), _
                GetChildItemInnerText(item, "comments"), _
                RssItemEnclosure.GetRssItemEnclosure(item), _
                GetChildItemInnerText(item, "guid"), _
                GetChildItemInnerTextDateTime(item, "pubDate"), _
                source)
        End Function


        Public Sub AddExtension(ByVal extension As RssExtension)
            If extensions Is Nothing Then extensions = New List(Of RssExtension)
            extensions.Add(extension)
        End Sub

        Public Sub AddCategory(ByVal cat As RssCategory)
            If category Is Nothing Then category = New List(Of RssCategory)
            category.Add(cat)
        End Sub

        ''' <summary>
        ''' Returns the RssItem as an Xml string
        ''' </summary>
        Public Function ToXmlString() As String
            Dim sb As New StringBuilder
            sb.Append("<item>")
            sb.Append(MakeItem("title", title))
            sb.Append(MakeItem("link", link.ToString))
            sb.Append(MakeItem("description", description))
            sb.Append(MakeItem("author", author))
            If category IsNot Nothing Then
                Dim c As RssCategory = Nothing
                For Each c In category
                    sb.Append(c.ToXmlString)
                Next
            End If
            sb.Append(MakeItem("comments", comments))
            If enclosure IsNot Nothing Then sb.Append(enclosure.ToXmlString)
            sb.Append(MakeItem("guid", guid))
            sb.Append(MakeItem("pubDate", pubDate.ToString("r")))
            If source IsNot Nothing Then sb.Append(source.ToXmlString)

            If extensions IsNot Nothing Then
                ' add the extensions
                Dim ext As RssExtension = Nothing
                For Each ext In extensions
                    sb.Append(ext.ToXmlString)
                Next
            End If
            sb.Append("</item>")

            Return sb.ToString

        End Function



#Region "Properties"

        ''' <summary>
        ''' The title of the item
        ''' </summary>
        Private _title As String
        Public Property title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        ''' <summary>
        ''' The url of the item
        ''' </summary>
        Private _link As Uri
        Public Property link() As Uri
            Get
                Return _link
            End Get
            Set(ByVal value As Uri)
                _link = value
            End Set
        End Property

        ''' <summary>
        ''' Content or abstract content from/about the item
        ''' </summary>
        Private _description As String
        Public Property description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property

        ''' <summary>
        ''' Author email or name (optional)
        ''' </summary>
        Private _author As String
        Public Property author() As String
            Get
                Return _author
            End Get
            Set(ByVal value As String)
                _author = value
            End Set
        End Property

        ''' <summary>
        ''' List of categories (optional)
        ''' </summary>
        Private _category As List(Of RssCategory)
        Public Property category() As List(Of RssCategory)
            Get
                Return _category
            End Get
            Set(ByVal value As List(Of RssCategory))
                _category = value
            End Set
        End Property

        ''' <summary>
        ''' url of the comments page for the item. (optional)
        ''' </summary>
        Private _comments As String
        Public Property comments() As String
            Get
                Return _comments
            End Get
            Set(ByVal value As String)
                _comments = value
            End Set
        End Property

        ''' <summary>
        ''' Describes a media object that is attached to the item. (optional)
        ''' </summary>
        Private _enclosure As RssItemEnclosure
        Public Property enclosure() As RssItemEnclosure
            Get
                Return _enclosure
            End Get
            Set(ByVal value As RssItemEnclosure)
                _enclosure = value
            End Set
        End Property

        ''' <summary>
        ''' A string that uniquely identifies the item. (optional)
        ''' </summary>
        Private _guid As String
        Public Property guid() As String
            Get
                Return _guid
            End Get
            Set(ByVal value As String)
                _guid = value
            End Set
        End Property

        ''' <summary>
        ''' Its value is a date, indicating when the item was published. (optional)
        ''' </summary>
        Private _pubDate As DateTime
        Public Property pubDate() As DateTime
            Get
                Return _pubDate
            End Get
            Set(ByVal value As DateTime)
                _pubDate = value
            End Set
        End Property

        ''' <summary>
        ''' Rss channel title and url meta (optional)
        ''' </summary>
        Private _source As RssItemSource
        Public Property source() As RssItemSource
            Get
                Return _source
            End Get
            Set(ByVal value As RssItemSource)
                _source = value
            End Set
        End Property


        Private _extensions As List(Of RssExtension)
        Public Property extensions() As List(Of RssExtension)
            Get
                Return _extensions
            End Get
            Set(ByVal value As List(Of RssExtension))
                _extensions = value
            End Set
        End Property


#End Region

    End Class



End Namespace